package com.itboye.ihomebank.activity.key;

import java.util.ArrayList;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ChooseCityListAdapter;
import com.itboye.ihomebank.db.DBOpenHelper;
/**
 * 备份钥匙搜索
 *
 */
public class ActivityKeySouSuo extends BaseOtherActivity {
	View v_statusbar;
	private ImageView close_icon;// 返回图片
	private TextView add_shap_title_tv;// 标题
	EditText edit_inputcity;
	
	ArrayList<String> arrayStrings = new ArrayList<>();
	private SQLiteDatabase database;
	protected Cursor cursor;
	ListView listview;
	ChooseCityListAdapter adapter;
	ImageView delete;
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;

		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		return R.layout.activity_key_sou_suo;
	}

	@Override
	protected void onInitController() {

	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("搜索钥匙");
		adapter = new ChooseCityListAdapter(this, arrayStrings);
		listview.setAdapter(adapter);
		database = SQLiteDatabase.openOrCreateDatabase(DBOpenHelper.DB_PATH + "/"+"databases"+"/"
				+ DBOpenHelper.KEYDB, null);//路径
		
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				edit_inputcity.setText(arrayStrings.get(position));
				Intent intent=new Intent();
				intent.putExtra("lock_alias", arrayStrings.get(position));
				setResult(103,intent);
				finish();
			}
		});
		
		edit_inputcity.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if(s.length()>0){
					delete.setVisibility(View.VISIBLE);
				}else
				{
					delete.setVisibility(View.GONE);
				}
				arrayStrings = new ArrayList<String>();
				// 创建模糊查询的条件
				String content = edit_inputcity.getText().toString();
				if (!TextUtils.isEmpty(content)) {
					String likeStr = "'";
					for (int i = 0; i < content.length(); i++) {
						if (i < content.length() - 1) {
							likeStr += "%" + content.charAt(i);
						} else {
							likeStr += "%" + content.charAt(i) + "%'";
						}
					}
					cursor = database.rawQuery(("select * from " + "key"
							+ " where lock_alias like " + likeStr), null);
					
					arrayStrings = queryInfo(cursor);
					adapter.refreshData(arrayStrings);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}
			@Override
			public void afterTextChanged(Editable s) {

			}
		});

	}
	public ArrayList<String> queryInfo(Cursor cursor) {
		try {
			while (cursor.moveToNext()) {
				arrayStrings.add(cursor.getString(cursor
						.getColumnIndex("lock_alias")));
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.toString();
		} finally {
			// database.close();
		}

		return arrayStrings;
	}
	@Override
	protected void onReadIntent(Bundle bundle) {

	}
	

}
